<template>
  <div id="main">
    <el-form :inline="true" :model="queryData">
      <el-form-item label="日期">
        <el-date-picker v-model="queryData.date" type="date" placeholder="选择日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="巡检任务">
        <el-select v-model="queryData.status" placeholder="请选择巡检任务">
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button class="greenButton" type="primary" @click="onQuery">查询</el-button>
        <el-button class="greenButton" type="primary" @click="reset">重置</el-button>
      </el-form-item>
      <el-form-item style="float:right;">
          <el-button type="primary" class="greenButton" @click="dleMany()">批量删除</el-button>
      </el-form-item>
    </el-form>

      <el-table
        border
        ref="multipleTable"
        @selection-change="handleSelectionChange"
        style="width: 100%;border:1px solid #00BDAC;"
        height="78%"
        :data="patrolSummaryList"
        :header-cell-style="{borderRight:'none',backgroundColor:'#00BDAC',color:'#FFFFFF',textAlign:'center'}"
      >
      <el-table-column
      type="selection"
      width="55">
      </el-table-column>
      <el-table-column align="center" width="50px" type="index"  :index="table_index" label="序号"></el-table-column>
        <el-table-column align="center" prop="date" label="巡检日期" width="120px"></el-table-column>
        <el-table-column align="center" prop="person" label="巡检人"></el-table-column>
        <el-table-column align="center" prop="route" label="巡检路线"></el-table-column>
        
        <el-table-column align="center" prop="bind" label="是否完成巡检任务">
          <template slot-scope="scope">
            <span :style="{color:scope.row.bind == '未完成'?'rgb(251, 28, 137)':''}">{{scope.row.bind}}</span>
            <!--<span style="color: #00BDAC;">详情</span>-->
          </template>
        </el-table-column>
        <el-table-column align="center" prop="desp" label="备注"></el-table-column>
        <el-table-column align="center"  label="操作">
          <template slot-scope="scope">
            <el-button @click="handleDelete(scope.$index, scope.row)" size="mini"
                     style="background-color: #FB1C89;color: #FFFFFF;">删除
          </el-button>
          </template>
        </el-table-column>
      </el-table>

    <div class="footer">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="pagesize"
        layout="total,prev, pager, next, jumper"
        :total="totalNum"
        prev-text="上一页"
        next-text="下一页"
      ></el-pagination>
      <el-button class="greenButton" @click="daochu" type="primary">导出/下载</el-button>
     
    </div>

    <el-dialog
      :visible.sync="isShowDetail"
      :show-close="false"
      class="detail-dialog"
      width="1100px"
    >
      <el-table
        :data="detailTable"
        border
        style="width: 100%"
        :header-cell-style="{borderRight:'none',backgroundColor:'#00BDAC',color:'#FFFFFF'}"
      >
      <el-table-column align="center" width="50px" prop="id" label="序号"></el-table-column>
        <el-table-column prop="person" label="巡检人"></el-table-column>
        <el-table-column prop="route" label="巡检路线"></el-table-column>
        <el-table-column prop="pointOne" label="1号巡检点"></el-table-column>
        <el-table-column prop="pointTwo" label="2号巡检点"></el-table-column>
        <el-table-column prop="pointThree" label="3号巡检点"></el-table-column>
        <el-table-column prop="status" label="巡检情况"></el-table-column>
      </el-table>
    </el-dialog>

    <el-dialog
      :visible.sync="isShowEdit"
      destroy-on-close
      :show-close="false"
      class="edit-dialog"
      width="1100px"
    >
      <div slot="title" class="edit-dialog-title">编辑巡查检任务</div>
      <el-form class="edit-form" label-width="100px" label-position="left">
        <el-form-item label="巡检时间：">
          <el-date-picker v-model="editFormData.date" type="date" placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="巡检人：">
          <el-select v-model="editFormData.person" placeholder>
            <el-option v-for="item in personList" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-for="(route,index) in editFormData.routes" :key="index" label="巡检路线：">
          <el-select v-model="editFormData.routes[index].route" placeholder>
            <el-option
              v-for="item in routeList"
              :key="item.value"
              :label="item.lable"
              :value="item.value"
            ></el-option>
          </el-select>
          <div v-if="index === 0" class="add-green-text">
            <img src="../../../../static/add.jpg" @click="addPatrolRoute" alt />
            <img src="../../../../static/cutdown.jpg" @click="reducePatrolRoute" alt />
            <span>添加巡检路线</span>
          </div>
        </el-form-item>
        <el-form-item label="备注要求：">
          <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="editFormData.tips"></el-input>
        </el-form-item>
        <el-form-item class="message-button">
          <el-button class="yellowButton">短信提醒</el-button>
        </el-form-item>
      </el-form>

      <div slot="footer">
        <el-button @click="cancel" class="pinkButton" type="primary">取消</el-button>
        <el-button @click="submit" class="greenButton" type="primary">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import DayJs from 'dayjs';
let queryData = {
  date: "",
  status: "",
  location: ""
};
export default {
  data: function() {
    return {
      queryData: {
        date: "",
        status: "",
      },

      statusOptions: [
        {
          value: "",
          label: "全部"
        },
        {
          value: "完成",
          label: "完成"
        },
        {
          value: "未完成",
          label: "未完成"
        }
      ],

      patrolSummaryList: [
        /*{
          date: "12.8",
          route: "一号线",
          buildingNo: "1号楼",
          person: "张三",
          status: "异常",
          roofNum:2,
          roomNo:'203',
          deviceNo:'000001',
          bind:'完成',
          deviceName:'打印机',
        },
        {
          date: "12.8",
          route: "二号线",
          buildingNo: "2号楼",
          person: "张四",
          status: "正常",
          roofNum:2,
          roomNo:'203',
          bind:'未完成',
          deviceNo:'000001',
          deviceName:'打印机',
        },
        {
          date: "12.8",
          route: "三号线",
          buildingNo: "3号楼",
          person: "张五",
          status: "异常",
          roofNum:2,
          roomNo:'203',
          bind:'未完成',
          deviceNo:'000001',
          deviceName:'电脑',
        },
        {
          date: "12.8",
          route: "一号线",
          buildingNo: "1号楼",
          person: "张三",
          status: "正常",
          roofNum:2,
          bind:'完成',
          roomNo:'203',
          deviceNo:'000001',
          deviceName:'电脑',
        },
        {
          date: "12.8",
          route: "一号线",
          buildingNo: "1号楼",
          person: "张三",
          status: "异常",
          roofNum:2,
          bind:'未完成',
          roomNo:'203',
          deviceNo:'000001',
          deviceName:'空调',
        },*/
      ],

      currentPage: 1,
      pagesize:10,
      totalNum:0,
      isShowDetail: false,
      shicklist:[],
      detailTable: [
        {
          person: "张三",
          route: "1号线",
          pointOne: "1号楼 正常签到",
          pointTwo: "2号楼 正常签到",
          pointThree: "3号楼 正常签到",
          status: "正常"
        }
      ],

      isShowEdit: false,

      editFormData: {
        date: "",
        person: "",
        routes: [
          {
            route: ""
          }
        ],
        tips: ""
      },

      personList: ["张三", "李四", "王五"],

      routeList: [
        {
          value: "1",
          label: "一号线"
        },
        {
          value: "2",
          label: "二号线"
        },
        {
          value: "3",
          label: "三号线"
        },
        {
          value: "4",
          label: "四号线"
        }
      ],
      multipleSelection:[]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    dleMany(){
      console.log(this.multipleSelection)
      if(this.multipleSelection.length==0){
        return this.$message.error('请选择您要删除的信息')
      }
      const arr = []
      this.multipleSelection.forEach((item)=>{
          arr.push(item.ta_id)
      })
      console.log(arr.join(','))
      this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let parme = {
                id:arr.join(',')
              }
              console.log(parme)
              this.ajaxCommon.ajax({
                    url: 'pilianggotaskdelete',
                    data:parme,
                    success: (res) => {
                      console.log(res)
                      if(res.code == 200){
                        this.$message({
                            message: '删除成功',
                            type: 'success',
                            duration:'2000'
                          });
                          this.getList()
                      }else{
                        this.$message({
                            message: res.status,
                            type: 'error',
                            duration:'2000'
                          });

                      }
                     
                    }
                });
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            });          
          });
    },
    handleSelectionChange(selection){
        console.log(selection)
        this.multipleSelection = selection
    },
    reset(){
      this.queryData.date = ''
      this.queryData.status = ''
       this.patrolSummaryList = this.shicklist;
      // this.onQuery()
    },
    handleDelete(index,row){
      this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let info = row.ta_id
            let url = `gotask_delete/${info}`
            console.log(url)
            this.ajaxCommon.ajax({
            url: 'tracedelTa',
            urlConcat:info,
            success: (res)=>{
              console.log(res);
              if(res.code==200){
                this.getList();
                // console.log(this.routeList)
                this.ajaxCommon.notice('删除成功');
              } else {
                this.ajaxCommon.notice(res.status,3);
              }
            }
          });
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            });          
          });
    },
    daochu(){
      window.location.href="http://hqgj.heweixiao.cn/export_gotask_list"
    },
    
    table_index(index){
        return (this.currentPage -1) * this.pagesize + index + 1
      },

    getList(){
      var param = {
        page: this.currentPage,
        sort: 'DESC'
      };
      if(queryData.date){
        param.date = queryData.date;
      }

      if(queryData.bind){
        param.bind = queryData.bind;
      }

      console.log(JSON.stringify(param));
      this.ajaxCommon.ajax({
        url: 'taskList',
        data: param,
        success: (res) => {
          console.log(res);
          var list = [];
          res.data.forEach(item=>{
            list.push(Object.assign(item, {
              date: item.ta_time,
              time: item.ta_time,
              route: item.route_name,
              person: item.user_nickname,
              bind: item.task_status===2?'完成':'未完成',
              status: item.equipment_status===1?'正常':'异常',
              desp: item.ta_des
            }))
          });
        
          this.patrolSummaryList = list;
          this.shicklist = list;
          this.totalNum = res.total;
        }
      });
    },
    onQuery() {
    
          if(this.queryData.date){
              let datatime = '';
              datatime = DayJs(this.queryData.date).format('YYYY-MM-DD');
              if(datatime && !this.queryData.name){
              let arr = [];
              this.shicklist.forEach(item => {
                if(datatime == item.date){
                  arr.push(item)
                }
              })
              this.patrolSummaryList = arr;
            }
          }  

          if(this.queryData.status){
            let arr = [];
            this.shicklist.forEach(item => {
              if(this.queryData.status == item.bind){
                arr.push(item)
              }
            })
            this.patrolSummaryList = arr; 
          }


           if(this.queryData.date && this.queryData.status){
              let datatime = '';
              datatime = DayJs(this.queryData.date).format('YYYY-MM-DD');
              if(datatime && !this.queryData.name){
              let arr = [];
              this.shicklist.forEach(item => {
                if(datatime == item.date && this.queryData.status == item.bind){
                  arr.push(item)
                }
              })
              this.patrolSummaryList = arr;
            }
          }  

          // if(this.queryData.status == ''){
          //   let arr = [];
          //   this.shicklist.forEach(item => {
              
          //       arr.push(item)
             
          //   })
          //   this.patrolSummaryList = arr;
          // }




    },
    onDetailclick(row) {
      this.isShowDetail = true;
      console.log(row);
    },
    handleRemind(row) {
      console.log(row);
    },
    handleEdit(row) {
      this.isShowEdit = true;
      console.log(row);
    },
    handleCancel(row) {
      console.log(row);
    },
    //分页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList();
    },
    addPatrolRoute() {
      this.editFormData.routes.push({
        route: ""
      });
    },
    reducePatrolRoute() {
      if (this.editFormData.routes.length > 1) this.editFormData.routes.pop();
    },
    submit(){
      this.isShowEdit = false;
    },
    cancel(){
      this.isShowEdit = false;
    }
  }
};
</script>

<style lang="less" scoped>
#main {
  width: 100%;
}

a {
  color: #00bdac;
  text-decoration: none;
}

.footer {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
}

.edit-dialog-title {
  color: #ffffff;
  padding-left: 30px;
  height: 36px;
  line-height: 36px;
  font-size: 14px;
  background: #00bdac;
}

.edit-form {
  width: 530px;
}

.message-button {
  text-align: right;
}

.add-green-text {
  display: inline-flex;
  color: #00bdac;
  width: 140px;
  justify-content: space-between;
  align-items: center;
  vertical-align: bottom;
  margin-left: 14px;

  img {
    cursor: pointer;
    width: 22px;
    height: 22px;
  }
}

.table-container{
  height: 420px;
  border:1px solid #00BDAC;
}
</style>

<style lang="less">
.detail-dialog {
  .el-dialog__header {
    padding: 0;
  }

  .el-dialog__body {
    padding: 0;
    height: 450px;
  }
}

.edit-dialog {
  .el-dialog__header {
    padding: 0;
  }
}
</style>
